#ifdef __cplusplus
extern "C" {
#endif

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>

#define MEM_NULL                         (0)

/**
 * @def MEM_ALIGN_UP(size, align)
 * 
 * Return the most contiguous size aligned at specified width. MEM_ALIGN_UP(13, 4)
 * would return 16.
 */
#define MEM_ALIGN_UP(size, align)           (((size) + (align) - 1) & ~((align) - 1))

/**
 * @def MEM_ALIGN_DOWN(size, align)
 * 
 * Return the down number of aligned at specified width. MEM_ALIGN_DOWN(13, 4)
 * would return 12.
 */
#define MEM_ALIGN_DOWN(size, align)      ((size) & ~((align) - 1))

typedef struct mem_block_t
{
    uint16_t size;
    uint16_t used;

    struct mem_block_t* next;
    struct mem_block_t* prev;
}mem_block_t;

typedef struct {
    uint32_t* head;
    uint32_t* tail;
    mem_block_t* list;   /* must pointer to the first block */
    uint32_t available_size;
    uint32_t block_size;
}mem_pool_t;

//void mem_init(void *begin_addr, void *end_addr);
int8_t mem_init(mem_pool_t* pool, uint32_t* head, uint32_t size, uint32_t unit);

//void *mem_malloc(uint32_t size); 
void* mem_malloc(mem_pool_t* pool, uint32_t size);
//void *mem_realloc(void *rmem, uint32_t newsize);
//
//void *mem_calloc(uint32_t count, uint32_t size);

int8_t mem_free(mem_pool_t* pool, void* mem);
//void mem_free(void *rmem);


#ifdef __cplusplus
}
#endif
